﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DSimulationApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApC3DSimulationApp
{
  private TxSimulationPlayer m_simulationPlayer;
  private CApC3DProcessNodeData m_rootOperation;
  private CApC3DProcessNodeData m_operationToRecord;
  private float m_currentOperationLastSavedTime;
  private float m_previousStepEndTime;
  private bool m_doesStartRecordOperation;
  private bool m_doesChangeCurrentOperation;
  private bool m_cancel;
  private ITxOperation m_currentOperation;
  private bool m_presentationMode;
  private bool m_exportSimulation;
  private TxExportGraphicsAnimationData m_exportData;

  internal event CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventHandler StartStepRecording;

  public TxExportGraphicsAnimationFileFormatEx Format { get; set; }

  public TxExportGraphicsAnimationData ExportData
  {
    set => this.m_exportData = value;
  }

  public bool ExportSimulation
  {
    get => this.m_exportSimulation;
    set => this.m_exportSimulation = value;
  }

  internal CApC3DSimulationApp()
  {
  }

  internal void Initialize(CApC3DProcessNodeData rootOperation)
  {
    this.m_rootOperation = rootOperation;
    this.m_currentOperation = TxApplication.ActiveDocument.CurrentOperation;
    this.m_presentationMode = TxApplication.Options.Simulation.PresentationMode;
    TxApplication.Options.Simulation.PresentationMode = false;
    this.m_simulationPlayer = TxApplication.ActiveDocument.SimulationPlayer;
    this.m_simulationPlayer.DoOnlyUnscheduledReset(false);
    this.m_simulationPlayer.AskUserForReset(false);
    this.m_cancel = false;
  }

  internal void Initialize(
    CApC3DProcessNodeData rootOperation,
    string fileName,
    string animationJSFileName,
    CApC3DDocumentationSettings documentationSettings,
    CApC3DAppearanceSet appearanceSet)
  {
    this.Initialize(rootOperation);
    try
    {
      TxExportGraphicsAnimationInitData animationInitData = new TxExportGraphicsAnimationInitData((ITxObject) TxApplication.ActiveDocument.PhysicalRoot, fileName, animationJSFileName, documentationSettings.Compress3DDocuments, documentationSettings.LevelOfDetails, this.m_exportSimulation);
      if (this.m_exportData != null)
      {
        animationInitData.ExportData = this.m_exportData;
        animationInitData.FileFormat = this.Format;
      }
      foreach (CApC3DAppearance allAppearance in appearanceSet.GetAllAppearances())
        animationInitData.AddAppearance(allAppearance.GetData());
      TxExportGraphicsAnimationEx.Init(animationInitData);
      animationInitData.Dispose();
    }
    catch (TxAcrobatProExtendedInitFailedException ex)
    {
      throw new ApplicationException(((Exception) ex).Message);
    }
    catch (TxException ex)
    {
      throw new ApplicationException(CApC3DUtils.GetResourceManager().GetString("CREATE_3D_DOCUMENTATION_U3D_FAILED"));
    }
  }

  internal bool Record(CApC3DProcessNodeData operationToRecord)
  {
    if (!this.m_cancel)
    {
      this.m_operationToRecord = operationToRecord;
      this.m_simulationPlayer.DoOnlyUnscheduledReset(false);
      TxApplication.ActiveDocument.CurrentOperation = (ITxOperation) null;
      TxApplication.ActiveDocument.CurrentOperation = this.m_rootOperation.NodeObject;
      this.m_simulationPlayer.Rewind();
      this.RegisterToSimulationEvents();
      this.m_doesStartRecordOperation = false;
      this.m_doesChangeCurrentOperation = false;
      if (this.m_exportData != null)
        this.m_simulationPlayer.PlayWithoutRefresh();
      else
        this.m_simulationPlayer.Play();
      this.UnRegisterFromSimulationEvents();
      if (this.m_doesStartRecordOperation)
      {
        this.EndStep();
        this.m_doesStartRecordOperation = false;
      }
      this.m_simulationPlayer.Rewind();
      this.m_simulationPlayer.DoOnlyUnscheduledReset(false);
    }
    return !this.m_cancel;
  }

  private void StartStep()
  {
    this.StartStepRecording((object) this, new CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventArgs(this.m_operationToRecord));
    TxExportGraphicsAnimationEx.StartStep(((ITxObject) this.m_operationToRecord.NodeObject).Name, this.m_previousStepEndTime);
  }

  private void EndStep()
  {
    this.m_previousStepEndTime += (float) this.m_simulationPlayer.CurrentTime;
    TxExportGraphicsAnimationEx.EndStep(((ITxObject) this.m_operationToRecord.NodeObject).Name, this.m_previousStepEndTime);
  }

  internal void Terminate()
  {
    this.m_rootOperation = (CApC3DProcessNodeData) null;
    this.m_operationToRecord = (CApC3DProcessNodeData) null;
    this.m_currentOperationLastSavedTime = 0.0f;
    this.m_previousStepEndTime = 0.0f;
    TxExportGraphicsAnimationEx.SaveAndTerminate();
    TxApplication.Options.Simulation.PresentationMode = this.m_presentationMode;
    TxApplication.ActiveDocument.CurrentOperation = this.m_currentOperation;
    this.m_simulationPlayer.AskUserForReset(true);
  }

  internal void Cancel()
  {
    this.m_cancel = true;
    this.UnRegisterFromSimulationEvents();
    this.m_doesStartRecordOperation = false;
    this.m_simulationPlayer.Stop();
  }

  private void RegisterToSimulationEvents()
  {
    this.m_simulationPlayer.OperationStartedForward += new TxSimulationPlayer_OperationStartedForwardEventHandler(this.OperationStartedForward);
    this.m_simulationPlayer.OperationEndedForward += new TxSimulationPlayer_OperationEndedForwardEventHandler(this.OperationEndedForward);
    this.m_simulationPlayer.SimulationEndedForward += new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.SimulationEndedForward);
    this.m_simulationPlayer.SimulationStopped += new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimulationStopped);
    this.m_simulationPlayer.TimeIntervalReached += new TxSimulationPlayer_TimeIntervalReachedEventHandler(this.TimeIntervalReached);
  }

  private void UnRegisterFromSimulationEvents()
  {
    this.m_simulationPlayer.OperationStartedForward -= new TxSimulationPlayer_OperationStartedForwardEventHandler(this.OperationStartedForward);
    this.m_simulationPlayer.OperationEndedForward -= new TxSimulationPlayer_OperationEndedForwardEventHandler(this.OperationEndedForward);
    this.m_simulationPlayer.SimulationEndedForward -= new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.SimulationEndedForward);
    this.m_simulationPlayer.SimulationStopped -= new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimulationStopped);
    this.m_simulationPlayer.TimeIntervalReached -= new TxSimulationPlayer_TimeIntervalReachedEventHandler(this.TimeIntervalReached);
  }

  internal void OperationStartedForward(
    object sender,
    TxSimulationPlayer_OperationStartedForwardEventArgs args)
  {
    if (this.m_operationToRecord.NodeObject != args.Operation || this.m_doesStartRecordOperation)
      return;
    this.m_doesStartRecordOperation = true;
    if (this.m_operationToRecord.NodeObject != this.m_rootOperation.NodeObject)
    {
      this.m_currentOperationLastSavedTime = 0.0f;
      this.m_simulationPlayer.Stop();
    }
    else
      this.StartStep();
  }

  internal void OperationEndedForward(
    object sender,
    TxSimulationPlayer_OperationEndedForwardEventArgs args)
  {
    if (this.m_operationToRecord.NodeObject != args.Operation || !this.m_doesStartRecordOperation)
      return;
    this.m_doesStartRecordOperation = false;
    this.EndStep();
    this.m_simulationPlayer.Stop();
  }

  internal void SimulationEndedForward(
    object sender,
    TxSimulationPlayer_SimulationEndedForwardEventArgs args)
  {
  }

  internal void SimulationStopped(object sender, TxSimulationPlayer_SimulationStoppedEventArgs args)
  {
    if (!this.m_doesStartRecordOperation || this.m_doesChangeCurrentOperation)
      return;
    this.UnRegisterFromSimulationEvents();
    this.m_simulationPlayer.DoOnlyUnscheduledReset(true);
    TxApplication.ActiveDocument.CurrentOperation = this.m_operationToRecord.NodeObject;
    this.m_doesChangeCurrentOperation = true;
    this.RegisterToSimulationEvents();
    this.StartStep();
    this.m_simulationPlayer.Play();
  }

  internal void TimeIntervalReached(
    object sender,
    TxSimulationPlayer_TimeIntervalReachedEventArgs args)
  {
    if (!this.m_doesStartRecordOperation)
      return;
    TxExportGraphicsAnimationEx.UpdateTimeInterval((float) args.CurrentTime - this.m_currentOperationLastSavedTime);
    this.m_currentOperationLastSavedTime = (float) args.CurrentTime;
  }

  internal delegate void CApC3DSimulationApp_StartStepRecordingEventHandler(
    object sender,
    CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventArgs args);

  internal class CApC3DSimulationApp_StartStepRecordingEventArgs : EventArgs
  {
    private CApC3DProcessNodeData m_step;

    internal CApC3DSimulationApp_StartStepRecordingEventArgs(CApC3DProcessNodeData step)
    {
      this.m_step = step;
    }

    internal CApC3DProcessNodeData Step => this.m_step;
  }
}
